<?php
class perm
{
	public $table;
	public $type_table;
	public $pages;
	public $db;
	public $error;
	public $current_user;
	
	function perm()
	{
		global $db,$current_user;
        $this->db = &$db;
        $this->table = DB_PRE.'perm';
        $this->type_table = DB_PRE.'type';
        $this->current_user = $current_user;
	}
	
	function get_all_perms()
	{
		
		$this->db->prepare('select p_id,p_name,pt_code,p_code from '.$this->table.' where status=? order by listorder desc,inputtime desc');
		$this->db->set_int(STATUS_NORMAL);
		$res = $this->db->execute();
		
		$list = array();
		if(is_array($res))
		{
			foreach ($res as $v)
			{
				if(!isset($list[$v['pt_code']]))
				{
					$list[$v['pt_code']] = array();
				}
				$list[$v['pt_code']][]=$v;
			}
		}
		
		if(!class_exists('type'))
		{
			require '../include/type.class.php';
		}
		$type = new type();
		$types = $type->get_typs('pct001');
		$type_list = array();
		if(is_array($types))
		{
			foreach ($types as $v)
			{
				if(isset($list[$v['tcode']]))
				{
					$type_list[$v['tcode']]['type'] = $v;
					$type_list[$v['tcode']]['perm'] = $list[$v['tcode']];
				}
			}
		}
		return $type_list;
	}
	
	function get_exist_perm($perms)
	{
		$list = array();
		if(!is_array($perms) || sizeof($perms) <=0)
		{
			return $list;
		}
		$size = sizeof($perms);
		$conditions = array_fill(0,$size,'?');
		$conditions = implode(',',$conditions);
		$this->db->prepare('select p_code from '.$this->table.' where p_code in ('.$conditions.') and status>?');
		foreach ($perms as $p)
		{
			$this->db->set_string($p);
		}
		$this->db->set_int(STATUS_DEL);
		$res = $this->db->execute();
		if(is_array($res))
		{
			foreach ($res as $v)
			{
				$list[] = $v['p_code'];
			}
		}
		return $list;
	}

    /**
	 * 添加权限元信息
	 *
	 * @param ARRAY() $data
	 * @return 	INT or FALSE
	 */
    public function add_perm( $data )
    {
        $params = $data;
        $res = $this->db->insert( $this->table, $params );
		return $res;
    }

    /**
	 * 获取单条权限元信息
	 *
	 * @param INT $permid
	 * @return ARRAY $perm
	 */
    public function get_perm( $permid )
    {
        $this->db->prepare( "SELECT * FROM $this->table WHERE status=? and p_id=? LIMIT 1" );
        $this->db->set_int( STATUS_NORMAL );
        $this->db->set_string( $permid );
		$perm = $this->db->execute( true );
		return $perm;
    }

    /**
	 * 修改权限元信息
	 *
	 * @param ARRAY $data
	 * @param INT $permid
	 * @return BOOL
	 */
    public function edit_perm( $data, $permid )
    {
        $params = $data;
		$res = $this->db->simple_update( $this->table, $params, array( 'p_id'=>$permid ) );
		return $res;
    }

    /**
	 * 删除权限元信息
	 *
	 * @param INT $pid
	 * @return BOOL
	 */
    public function del_perm( $pid )
    {
        return $this->edit_perm( array( 'status'=>STATUS_DEL ), $pid );
    }

    /**
	 * 查询所有权限元列表信息
	 *
	 * @param ARRAY() $params, STRING $order, INT $page, INT $pagesize
	 * @return 	ARRAY() $list
	 */
    public function get_list( $params, $order, $page=1, $pagesize=10 )
    {
        $where = '';
        if( is_array( $params ) && sizeof( $params ) > 0 )
        {
            $where = ' where ';
            $where .= implode( ' and ', $params );
        }
        $where .= $order;
        $this->db->prepare( "SELECT count(*) as nums FROM $this->table" . $where );
		$res = $this->db->execute( true );
		$nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
        $this->pages = pages( $nums, $page, $pagesize );
		$this->db->prepare( "SELECT * FROM $this->table" . $where . " limit ?, ?" );
		$this->db->set_int( ( $page-1 ) * $pagesize );
		$this->db->set_int( $pagesize );
	    $list = $this->db->execute();
        return $list;
    }

    /**
	 * 权限元信息排序
	 *
	 * @param ARRAY() $data
	 * @return 	BOOL
	 */
    public function listorder( $data )
    {
        if( !is_array( $data ) )
        {
            return false;
        }
        foreach( $data as $permid => $listorder )
        {
            $listorder = intval( $listorder );
            $this->edit_perm( array( 'listorder' => $listorder ), $permid );
        }
        return true;
    }

    /**
	 * 获得所有权限元分类编码、权限元分类名称
	 *
	 * @param 空
	 * @return array $list
	 */
    public function get_all_perm_type()
    {
    	if(!class_exists('type'))
		{
			require '../include/type.class.php';
		}
		$type = new type();
		$types = $type->get_typs('pct001');
		$type_list = array();
		if(is_array($types))
		{
			foreach ($types as $v)
			{
				$type_list[$v['tcode']] = $v['tname'];
			}
		}
    	return $type_list;
    }
}
?>